package com.sl.au.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.ProductPlanSonView;
import com.sl.au.entity.TbOPProductPlanView;
import com.sl.au.service.ProductPlanService;
import com.sl.au.service.ProductPlanSonViewService;

@RestController
@RequestMapping(value="/ui/productPlan")
public class ProductPlanController extends BaseController{
	
	@Autowired
	ProductPlanService productPlanService;
	
	@Autowired
	ProductPlanSonViewService productPlanSonViewService;
	
	@RequestMapping(method = RequestMethod.GET)
    public ModelAndView index(){
        ModelAndView modelAndView=createLayoutView("publicFunction/productPlanIndex");
        return modelAndView;
    }
	
	@RequestMapping(value = "/getpage", method = RequestMethod.GET)
	public Map<String, Object> getpage(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String,Object>();
		Account a = this.getMyAccount();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
        List<TbOPProductPlanView> users = null;
		users = productPlanService.getMyPage(start, length, key);
		long count =0;		
		count=productPlanService.getCount(key);
		System.out.println(count);
		map.put("data", users);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	@RequestMapping(value = "/getProduct", method = RequestMethod.GET)
	public Map<String, Object> getProduct(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String,Object>();
		Account a = this.getMyAccount();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		int key = 0;
		String aaa = request.getParameter("id");
		if(request.getParameter("id") == null){
			key = 0;
		}else{
			key = Integer.parseInt(request.getParameter("id"));
		}
        List<ProductPlanSonView> users = null;
		users = productPlanSonViewService.getMyPage(start, length, key);
		long count =0;		
		count=productPlanSonViewService.getCount(key);
		System.out.println(count);
		map.put("data", users);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
}
